﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Training.Library.DataAccess.Administration.Catalog;
using System.Configuration;
using Microsoft.Reporting.WebForms;
using Training.Library.DomainObject.Administration.Catalog;
using Training.Library.DataAccess.Transaction;


namespace Training.Web.Reports 
{

    public partial class rptTransPelatihanList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                PelatihanRepo PelatihanDal = new PelatihanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Pelatihan> list = PelatihanDal.GetList();

                CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                Cabang objCabang = CabangDal.GetById(base.CabangID);
                if (objCabang != null)
                {
                    if (!objCabang.NamaCabang.Trim().ToLower().Contains("pusat"))
                    {
                        var query = list.Where(x => x.CabangID == base.CabangID).OrderBy(x => x.NamaPelatihan);
                        list = query.ToList();
                    }
                    else
                    {
                        var query = list.OrderBy(x => x.NamaPelatihan);
                        list = query.ToList();
                    }

                }

               
                ddlPelatihan.Items.Clear();

                list.ForEach(delegate(Pelatihan obj)
                {
                    ddlPelatihan.Items.Add(new ListItem(obj.NamaPelatihan, obj.PelatihanID.ToString()));
                });

                ddlPelatihan.Items.Insert(0, new ListItem("-- pilih pelatihan --", string.Empty));


                PembicaraRepo PembicaraDal = new PembicaraRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Pembicara> listPembicara = PembicaraDal.GetList();
                var queryPembicara = listPembicara.OrderBy(x => x.NamaPembicara);
                listPembicara = queryPembicara.ToList();
                ddlPembicara.Items.Clear();

                listPembicara.ForEach(delegate(Pembicara objPembicara)
                {
                    ddlPembicara.Items.Add(new ListItem(objPembicara.NamaPembicara, objPembicara.PembicaraID.ToString()));
                });

                ddlPembicara.Items.Insert(0, new ListItem("-- semua pembicara --", string.Empty));

                KaryawanRepo PesertaDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                List<Karyawan> listPeserta = PesertaDal.GetList();
                var queryPeserta = listPeserta.OrderBy(x => x.NamaKaryawan);
                listPeserta = queryPeserta.ToList();
                ddlPeserta.Items.Clear();

                listPeserta.ForEach(delegate(Karyawan objPeserta)
                {
                    ddlPeserta.Items.Add(new ListItem(objPeserta.NIK + " - " + objPeserta.NamaKaryawan, objPeserta.NIK.ToString()));
                });

                ddlPeserta.Items.Insert(0, new ListItem("-- semua peserta --", string.Empty));

                //PelatihanRepo PelatihanDal = new PelatihanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                //DataTable dt = PelatihanDal.GetDataTableReport();

                //rsViewer.Reset();
                //rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/rptPelatihan.rdlc");
                //ReportDataSource rds = new ReportDataSource();
                //rds.Name = "DataSet1";
                //rds.Value = dt;

                //rsViewer.LocalReport.DataSources.Clear();
                //rsViewer.LocalReport.DataSources.Add(rds);
                //rsViewer.LocalReport.Refresh();

                //rsViewer.SetExportFormatVisibility(ReportViewerExportFormat.Excel, false);
                //rsViewer.SetExportFormatVisibility(ReportViewerExportFormat.Word, false);
            }
        }

        protected void btnProses_Click(object sender, EventArgs e)
        {


            if (ddlPelatihan.SelectedIndex == 0)
            {
                ClientScript.RegisterClientScriptBlock(typeof(Page), "success", "alert('" + "Pilih pelatihan sebagai parameter laporan !" + "');", true);
                return;
            }

            PelatihanDetailRepo PelatihanDetailDal = new PelatihanDetailRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
            string param = string.Empty;
            if (ddlPelatihan.SelectedIndex != 0)
                param = "WHERE A.PelatihanID=" + ddlPelatihan.SelectedItem.Value;
            if (ddlPembicara.SelectedIndex != 0)
                param = param == string.Empty ? "WHERE D.PembicaraID=" + ddlPembicara.SelectedItem.Value : param + " AND D.PembicaraID=" + ddlPembicara.SelectedItem.Value;
            if (ddlPeserta.SelectedIndex != 0)
                param = param == string.Empty ? " WHERE C.NIK=" + ddlPeserta.SelectedItem.Value : param + " AND C.NIK=" + ddlPeserta.SelectedItem.Value;
          
            DataTable dt = PelatihanDetailDal.GetDataTableReport(param);
            rsViewer.Reset();
            rsViewer.LocalReport.ReportPath = Server.MapPath("~/reportviewer/rptTransPelatihan.rdlc");
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "DataSet1";
            rds.Value = dt;

           

            rsViewer.LocalReport.EnableExternalImages = true;
            rsViewer.LocalReport.DataSources.Clear();
            rsViewer.LocalReport.DataSources.Add(rds);
            rsViewer.LocalReport.Refresh();

           
        }
    }
}